import org.eclipse.paho.client.mqttv3.*;

public class EmqxTopicList {
    public static void main(String[] args) {
        String broker = "tcp://139.196.109.54:1883"; // 替换为你的emqx主机地址
        String clientId = "emqx-subscriber";
        String topic = "$SYS/brokers/+/clients/#"; // 订阅所有主题

        try {
            MqttClient client = new MqttClient(broker, clientId);
            client.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    System.out.println("Connection lost: " + cause.getMessage());
                }

                @Override
                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    System.out.println("Received message on topic: " + topic);
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                }
            });

            client.connect();
            client.subscribe(topic);

            // 等待一段时间以收集主题信息
            Thread.sleep(5000);

            client.disconnect();
        } catch (MqttException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
